package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.InvCert;
import com.ysd.lis.entity.inv.InvManufacturerCert;
import com.ysd.lis.mapper.inv.InvManufacturerCertMapper;
import com.ysd.lis.request.inv.InvManufacturerCertDto;
import com.ysd.lis.request.inv.InvManufacturerCertParam;
import com.ysd.lis.service.inv.InvManufacturerCertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-09-11
 */
@Service
public class InvManufacturerCertServiceImpl extends ServiceImpl<InvManufacturerCertMapper, InvManufacturerCert> implements InvManufacturerCertService {
    @Autowired
    InvManufacturerCertMapper invManufacturerCertMapper;

    @Override
    public Result findInvManufacturerCertList(InvManufacturerCertParam param) {
        MPJLambdaWrapper<InvManufacturerCert> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(InvManufacturerCert.class)
                .selectAs("a", InvCert::getCertName,InvManufacturerCert::getCertName)
                .leftJoin(InvCert.class, "a", p -> p.eq(InvCert::getId, InvManufacturerCert::getCertId).eq(InvCert::getDelFlag, 0));

        if (StringUtils.isNotBlank(param.getManuId())) {
            queryWrapper.eq(InvManufacturerCert::getManuId,param.getManuId());
        }

        queryWrapper.eq(InvManufacturerCert::getDelFlag, 0)
                .orderByAsc(InvManufacturerCert::getSeq);
        return Result.succ(1, "查询成功", invManufacturerCertMapper.selectList(queryWrapper));
    }

    @Override
    public Result saveOrUpdateInvManufacturerCert(InvManufacturerCertDto dto) {
        List<InvManufacturerCert> manufacturerCertList = dto.getManufacturerCertList();
        boolean b = this.saveOrUpdateBatch(manufacturerCertList);
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }
    }

    @Override
    public Result batchDeleteInvManufacturerCertList(InvManufacturerCertDto dto) {
        List<InvManufacturerCert> manufacturerCertList = dto.getManufacturerCertList();

        List<String> ids = manufacturerCertList.stream().map(InvManufacturerCert::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
